[<<Previous Entry]
[^^Up^^]
[Next Entry>>]
[Menu]
[About The Guide]
atof() Convert String to Double
#include <math.h>
#include <stdlib.h> Use either math.h or stdlib.h
double atof(string); Value of converted string
const char *string; String to be converted
atof() converts 'string' to a double-precision, floating-point value.
'string' is a sequence of characters that can be interpreted as a
numeric value; it has the following format:
[whitespace][sign][digits][.digits][{e|E}[sign]digits]
where:
whitespace any number of tab and space characters are ignored
sign + or -
digits one or more decimal digits
e|E exponent prefixes
A decimal point must be followed by a digit; if there are no digits
before the decimal point, then at least one digit must appear after
it. atof() stops reading input at the first character it cannot
recognize as part of the number (likely the null character
terminating the string).
Returns: The double value of 'string'. If 'string' cannot be
converted to a double value, then 0.0 is returned.
The return value is undefined for overflow; 0.0 is
returned on underflow.
-------------------------------- Example ---------------------------------
The following statements convert several strings to double values and
print the values.
#include <math.h>
#include <stdio.h>
main()
{
double x, y, z;
x = atof(" -.625e-3 (this will be -0.000625)");
y = atof("0");
z = atof(" 143248291283472348923874.2134762734E222\n");
/* prints: x=-6.250000e-004, y=0.000000e+000, z=1.432483e+245 */
printf("x=%e, y=%e, z=%e\n", x, y, z);
}
See Also:
atoi()
atol()
ecvt()
fcvt()
gcvt()
This page created by ng2html v1.05, the Norton guide to HTML conversion utility.
Written by Dave Pearson